/**
 * 画布相关计算
 */
/**
 * 计算圆形布局
 *
 * @param options 计算圆形布局的参数对象
 * @param options.width 画布的宽度，默认为420
 * @param options.height 画布的高度，默认为300
 * @param options.padding 画布边缘的内边距，默认为10
 * @param options.horizontalSpacing 圆之间的水平间距，默认为10
 * @param options.verticalSpacing 圆之间的垂直间距，默认为5
 * @param options.rows 圆的行数
 * @param options.cols 圆的列数
 * @returns 返回一个包含半径和圆心坐标的对象
 */
export function calculateCircleLayout({ 
    width = 420, 
    height = 300, 
    padding = 10, 
    horizontalSpacing = 10, 
    verticalSpacing = 5, 
    rows, 
    cols 
}) {
    // 计算可用空间
    const availableWidth = width - 2 * padding;
    const availableHeight = height - 2 * padding;

    // 计算总间距
    const totalHorizontalSpacing = (cols - 1) * horizontalSpacing;
    const totalVerticalSpacing = (rows - 1) * verticalSpacing;

    // 计算半径（保证不超过可用空间）
    const horizontalRadius = (availableWidth - totalHorizontalSpacing) / (2 * cols);
    const verticalRadius = (availableHeight - totalVerticalSpacing) / (2 * rows);
    const radius = Math.min(horizontalRadius, verticalRadius);

    // 计算水平居中需要的偏移量（核心修改点）
    const totalRowWidth = cols * 2 * radius + (cols - 1) * horizontalSpacing;
    const horizontalOffset = (availableWidth - totalRowWidth) / 2;

    // 计算垂直居中需要的偏移量（保持对称）
    const totalColumnHeight = rows * 2 * radius + (rows - 1) * verticalSpacing;
    const verticalOffset = (availableHeight - totalColumnHeight) / 2;

    // 计算圆心坐标
    const centers = [];
    for (let row = 0; row < rows; row++) {
        for (let col = 0; col < cols; col++) {
            // 添加水平偏移实现居中
            const x = padding + horizontalOffset + radius + col * (2 * radius + horizontalSpacing);
            // 添加垂直偏移实现对称
            const y = padding + verticalOffset + radius + row * (2 * radius + verticalSpacing);
            centers.push({ x, y });
        }
    }

    return {
        radius: radius,
        centers: centers
    };
}

// // 示例：420x300 矩形，内边距 10，横向间距 10，纵向间距 5
// const width = 420;
// const height = 300;
// const padding = 10;
// const horizontalSpacing = 10;
// const verticalSpacing = 5;

// // 2排2列
// const layout2x2 = calculateCircleLayout(width, height, padding, horizontalSpacing, verticalSpacing, 2, 2);
// console.log('2排2列:', layout2x2);

// // 3排3列
// const layout3x3 = calculateCircleLayout(width, height, padding, horizontalSpacing, verticalSpacing, 3, 3);
// console.log('3排3列:', layout3x3);